package com.cx.controller.admin.member;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.csv.CSVPrinter;
import org.cx.base.ApiModel;
import org.cx.base.BaseController;
import org.cx.constant.BasicConstants;
import org.cx.constant.ServerCodeType;
import org.cx.entity.Page;
import org.cx.entity.PageData;
import org.cx.util.AssertUtil;
import org.cx.util.CommonsCsvUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cx.service.RedisService;
import com.cx.service.admin.member.ChainMemberManager;
import com.cx.utils.RedisKeyUtil;

@RestController("admin/basic/member")
@RequestMapping("admin/basic/member")
public class MemberController extends BaseController {

    @Resource
    private ChainMemberManager chainMemberService;

    @Resource
    private RedisService jedisService;


    @ResponseBody
    @RequestMapping(value = "/findMemberDetailPage")
    public ApiModel findMemberDetailPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Page pg =this.getPage();
        PageData pd = (PageData) pg.getPd();
        String token = request.getHeader("token");
        if(pd.get("orgId")== null || pd.get("orgId").equals("")){
            pd.put("orgId",jedisService.getSession(RedisKeyUtil.getLoginUserKey(token)).get("orgId"));
        }
        pd.put("topOrgId",jedisService.getSession(RedisKeyUtil.getLoginUserKey(token)).get("topOrgId"));
        pg.setPd(pd);
        List<PageData> list = chainMemberService.findMemberDetailPage(pg);
        pg.setPd(list);
        return buildSuccMsg(pg);
    }

    @ResponseBody
    @RequestMapping(value = "/updateMember")
    public ApiModel updateMember() throws Exception {
        PageData pd =this.getPageData();
        Integer result = chainMemberService.updateMember(pd);
        //   pg.setPd(list);
        return buildSuccMsg(pd);
    }

    @ResponseBody
    @RequestMapping(value = "/insertMember")
    public ApiModel insertMember() throws Exception {
        PageData pd =this.getPageData();
        Integer result  = chainMemberService.insertMember(pd);
        //   pg.setPd(list);
        return buildSuccMsg(pd);
    }

    @ResponseBody
    @RequestMapping(value = "/exportMemberDetail")
    public void exportMemberDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Page pg =this.getPage();
        PageData pd = (PageData) pg.getPd();
        AssertUtil.isNotNull(getUserByToken(request.getParameter("token")), ServerCodeType.USER_NOT_LOGIN.getMessage());
        if(pd.get("orgId")== null || pd.get("orgId").equals("")) {
            pd.put("orgId", getUserByToken(request.getParameter("token")).get("orgId"));
        }
        pd.put("topOrgId", getUserByToken(request.getParameter("token")).get("topOrgId"));
        pg.setPd(pd);
        pg.setShowCount(BasicConstants.ExportData.maxNum);
        List<PageData> list = chainMemberService.findMemberDetailPage(pg);
        
        String[] title ={"手机号码","姓名","性别","注册时间","注册途径"};
        String[] onlyFilds = {"phone","name","sexCh","createTime","source"};
        //文件名称
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = "memberDetail_"+dateFormat.format(new Date());
        //CSV导出，速度更快
        OutputStream outputStream =CommonsCsvUtil.getOutputStream(fileName, response);
        CSVPrinter csvPrinter = CommonsCsvUtil.getCSVPrinter(title, outputStream);
        CommonsCsvUtil.exportData(onlyFilds, list, csvPrinter);
        CommonsCsvUtil.close(outputStream, csvPrinter);
        
       // ExcelUtils.fillExcelForFile(list,title,onlyFilds,"memberDetail_",response);

    }





}

